package com.ruoyi.auth.utils;

import com.ruoyi.common.core.constant.RocketMQConstants;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.rocketmq.common.message.Message;
import org.springframework.stereotype.Component;

@Component
public class RocketMQUtils {

    @Autowired
    RocketMQTemplate rocketMQTemplate;

    @Autowired
    DefaultMQProducer defaultMQProducer;

    public boolean sendString(String topic, String message) {
        try {
            rocketMQTemplate.convertAndSend(topic, message);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean sendObject(String topic, Object message) {
        try {
            rocketMQTemplate.convertAndSend(topic, message);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public SendResult sendMessage(String topic, String message) {
        try {
            Message sendMsg = new Message(topic, RocketMQConstants.GROUP, message.getBytes());
            return defaultMQProducer.send(sendMsg);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
